package com.foreveross.bsl.auth.integration;

import java.util.Map;

import javax.inject.Inject;

import com.foreveross.bsl.auth.AuthAccount;
import com.foreveross.bsl.auth.AuthUser;
import com.foreveross.bsl.auth.service.IdentityService;
import com.foreveross.bsl.common.utils.context.ContextThead;

/**
 * @description
 * 
 * @author rening
 * @version v1.0
 * 
 * @2013-7-22
 * 
 */
public abstract class AbstractChannelIntegration implements ChannelIntegration {

	@Inject
	protected IdentityService identityService;

	public Object getInfo(String sessionKey) {
		return identityService.validateTicket(sessionKey);
	}

	public boolean validateTicket(String ticket) {
		Object vo = identityService.validateTicket(ticket);
		if (vo != null) {
			return true;
		}
		return false;
	}

	public boolean validateIP(String ip) {
		return true;
	}

	public boolean validateIPFlow(String ip) {
		return true;
	}

	public boolean validateIPResource(String ip, String resource) {
		return true;
	}

	public boolean logResourceUsage(ResourceUsage resourceUsage) {
		return true;
	}
	
	/**
	 * 校验 登录标识符
	 */
	@SuppressWarnings("rawtypes")
	public boolean validateIdentity(String sessionKey, Map parameters) {
		Object vo = identityService.validateTicket(sessionKey);
		if (vo != null) {
			if (vo instanceof AuthUser) {
				AuthUser user = (AuthUser) vo;
				ContextThead.setThreadVar(user.getAccountUsername());
			} else {
				AuthAccount account = (AuthAccount) vo;
				ContextThead.setThreadVar(account.getAccountUsername());
			}
			return true;
		}
		return false;
	}

}
